package com.cby.jetpack.sample.paging3.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.cby.jetpack.sample.R
import com.cby.jetpack.sample.databinding.ItemGirlBinding
import com.cby.jetpack.sample.paging3.bean.GankGirlBean

/**
 * @description: GankGirlListAdapter
 *
 * @author ChenBaoyang
 * @date 8/12/21 14:42
 */
class GankGirlListAdapter(val itemCallback:(Int, GankGirlBean?, GankGirlListAdapter) -> Unit) :
    PagingDataAdapter<GankGirlBean, GankGirlListAdapter.GirlViewHolder>(object :
    DiffUtil.ItemCallback<GankGirlBean>() {

    override fun areItemsTheSame(
        oldItem: GankGirlBean,
        newItem: GankGirlBean
    ): Boolean {
        return oldItem.id == newItem.id
    }

    @SuppressLint("DiffUtilEquals")
    override fun areContentsTheSame(
        oldItem: GankGirlBean,
        newItem: GankGirlBean
    ): Boolean {
        return oldItem == newItem
    }
}) {


    override fun onBindViewHolder(holder: GirlViewHolder, position: Int) {
        val girlBean = getItem(position)
        holder.binding.girlBean = girlBean
        holder.itemView.setOnClickListener {
            itemCallback(position, girlBean, this@GankGirlListAdapter)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GirlViewHolder {
        return GirlViewHolder(DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_girl,
            parent,
            false))
    }

    inner class GirlViewHolder(private val dataBinding: ItemGirlBinding) :
        RecyclerView.ViewHolder(dataBinding.root) {
        var binding = dataBinding

    }
}